<?php

class ResourceController extends FController
{
	public function actionUploadYB()
	{
        if(isset($_GET['htmlId'])) {
            setSession('YBHtmlIDText',$_GET['htmlId']);
        }
        $this->layout = 'little';

        $model = new YbFormModel();

        $form = TbForm::createForm($model->getFormConfig(),$model,
                    array( //@see TbActiveForm attributes
                        'htmlOptions'=>array('class'=>'well'),
                        'type'=>'horizontal', //'inline','horizontal','vertical'
                    )
                );
        if($form->submitted() && $form->validate())
        {
            $data = $_POST['YbFormModel'];
            setSession('YBData',$data);
            Yii::import('common.vendors.*');
            require_once 'Zend/Loader.php';
            Zend_Loader::loadClass('Zend_Gdata_YouTube');
            Zend_Loader::loadClass('Zend_Gdata_ClientLogin');

            $authenticationURL = settings()->get('system','yb_authenticationURL');

            $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
                $username          = settings()->get('system','yb_username'),
                $password           = settings()->get('system','yb_password'),
                $service           = settings()->get('system','yb_service'),
                $client           = null,
                $source           = settings()->get('system','yb_source'),
                $loginToken           = null,
                $loginCaptcha      = null,
                $authenticationURL);

            $devkey = settings()->get('system','yb_devkey');

            $yt = new Zend_Gdata_YouTube($httpClient, '', '', $devkey);

            $video = new Zend_Gdata_YouTube_VideoEntry();
            $video->setVideoTitle($data['name']);
            $video->setVideoDescription($data['description']);
            $video->setVideoPrivate();
            $video->setVideoCategory(settings()->get('system','yb_category')); // see Youtube. Else you may get an error. Avoid using People & Blogs. People alone or Blogs alone is good.
            $video->SetVideoTags($data['video_tags']);
            $handler_url     = settings()->get('system','yb_handler_url');
            $token_array     = $yt->getFormUploadToken($video, $handler_url);
            $token          = $token_array['token'];
            $post_url     = $token_array['url'];
            $next_url      = hostInfo().url('/resource/receiveYBInfo');
            $this->render('uploadYB',array('token'=>$token,'post_url'=>$post_url,'next_url'=>$next_url));
        }else {
            $this->renderText($form);
        }
	}
    public function actionReceiveYBInfo(){
        $this->layout = 'little';
        $htmlID = getSession('YBHtmlIDText');
        $status = 400;
        if(isset($_GET['status'])) {
            $status = $_GET['status'];
        }
        if($status == 200) {
            $linkYB = 'http://www.youtube.com/watch?v='.$_GET['id'];
            if($htmlID) {
                $data = getSession('YBData');
                $js = "function sendYBId()
               {
                   var htmlID = '".$htmlID."';
                   var htmlIDName = htmlID.replace('_url_','_name_');
                   var htmlIDDes = htmlID.replace('_url_','_description_');
                   window.opener.document.getElementById('".$htmlID."').value='".$linkYB."';
                   window.opener.document.getElementById(htmlIDName).value='".$data['name']."';
                   window.opener.document.getElementById(htmlIDDes).value='".$data['description']."';
                   console.log(window.opener.document.getElementById(htmlIDName));
                   console.log(window.opener.document.getElementById(htmlIDDes));
                   close();
               }
               setInterval(function(){
                sendYBId();
                }, 5000)
               ";
                cs()->registerScript('sendYBId',$js,2);
                $content =  "<div style='margin-top:50px' class='text-center'>
                                <div style='margin-top:50px' class='alert alert-success'>
                                <button type='button' class='close' data-dismiss='alert'>&times;</button>
                                <b>".t('Oh yeah !','resource')."</b>
                                ".t('Upload videos to youtube success','resource')."
                                </div>
                                <div class='loading-53'></div>
                                <div><a href='#' onclick='js:sendYBId();'>".t('Kick here , if the page does not automatically redirect')."</a> </div>
                            </div>";
                $this->renderText($content);
            } else {
                $content =  "<div style='margin-top:50px' class='text-center'>
                                <div style='margin-top:50px' class='alert alert-success'>
                                <button type='button' class='close' data-dismiss='alert'>&times;</button>
                                <b>".t('Oh yeah !','resource')."</b>
                                ".t('Upload videos to youtube success','resource')."
                                </div>
                                <div class='loading-53'></div>
                                <div><a href='#' onclick='js:sendYBId();'>".t('Kick here , if the page does not automatically redirect')."</a> </div>
                            </div>";
                $this->renderText($content);
            }
        } else {
            $content = '<div style="margin-top:50px" class="alert alert-error">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <b>'.t('Oh snap !','resource').'</b>
            '.t('Error While trying to upload video','resource').'
            </div>';
            $this->renderText($content);
        }
    }

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}